<?php
namespace php\geo\googlemaps;

use php\util\CURLClient;
use php\i18n\Locale;
use php\mvc\App;

class GeoCode {

    protected static $PROTOCOL = "http";
    protected static $HOST = "maps.google.com";
    protected static $PATH = "/maps/api/geocode/";
    protected static $TYPE = "xml";

    public function __construct() {
    }

    /*
     * @return GeoCodeResponse object
     */
    public function query($text) {
        $curl = new CURLClient();
        $url = $this->createURL($text);
        $data = $curl->get($url);
        $response = new GeoCodeResponse($data);
        return $response;
    }

    private function createURL($text) {
        $url = sprintf("%s://%s%s%s?sensor=false&address=%s&language=%s",
        self::$PROTOCOL, self::$HOST, self::$PATH, self::$TYPE
        , urlencode(App::encode($text)), $this->getLanguage());
        return $url;
    }

    private function getLanguage() {
        $default = Locale::getDefault();
        return $default->getLanguage();
    }
}
